<?php 
class Admin_AdvertisementcategoryController extends Zend_Controller_Action{
	public $_model = null;
	
	public function init(){
		$this->_model = new Application_Model_DbTable_AdvertisementCategory();
		
	}
	
	public function indexAction(){
		$filters = array();
		
		//từ khóa
		$keyword = $this->_request->getParam('keyword','');
		$filters['keyword']=$keyword;
		$this->view->keyword = $keyword;
		
		// từ ngày
		$fromDate = $this->_request->getParam('fromDate',null);
		//$filters['fromDate']=Cms_String_String::getIntFromDate($fromDate);
		$filters['fromDate']=Cms_String_String::getDateFromString($fromDate);
		$this->view->fromDate = $fromDate;
		
		// đến ngày
		$toDate = $this->_request->getParam('toDate',null);
		$filters['toDate']=Cms_String_String::getDateFromString($toDate,'last');//chuyển về định dạng của mysql
		$this->view->toDate = $toDate;
		
		// trạng thái | status
		$status = $this->_request->getParam('searchStatus',"");
		$filters['status']=$status;
		$this->view->status=$status;
		
		// sắp xếp | order
		$order = $this->_request->getParam('order',"");
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
		
		
		//get item count per page
		$icpp = $this->_request->getParam('icpp',20);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
		$list = $this->_model->getList($filters,$orderType,$orderBy);
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
		
	}
	
	public function insertAction(){
		$form = new Admin_Form_Advertisementcategory();
		if($this->_request->isPost()){
			if($form->isValid($_POST)){
				$row = $this->_model->createRow($_POST);
				$row->advertisement_category_alias = Cms_String_String::toAlias($row->advertisement_category_name);
				$row->save();
				$this->_helper->flashMessenger('Thêm thành công!');
				$this->_helper->redirector('index','advertisementcategory','admin');
			}
		}
		$this->view->form = $form;
	}
	
	public function updateAction(){
		$form = new Admin_Form_Advertisementcategory();
		$id = $this->_request->getParam('id');
		$row  = $this->_model->find($id)->current();		
		if($this->_request->isPost()){
			if($form->isValid($_POST)){
				
				$row->advertisement_category_name = $form->getValue('advertisement_category_name');
				$row->advertisement_category_alias = Cms_String_String::toAlias($form->getValue('advertisement_category_name'));				
				$row->advertisement_category_description_vi = $form->getValue('advertisement_category_description_vi');
				$row->advertisement_category_description_en = $form->getValue('advertisement_category_description_en');				
				
				$row->save();
				$this->_helper->flashMessenger('Cập nhật thành công!');
				$this->_helper->redirector('index','advertisementcategory','admin');
			}
		}
		$form->populate($row->toArray());
		$this->view->form = $form;
	}
	
	public function deleteAction(){
// 		$status = 0;
// 		$id = @(array)$this->_request->getParam('id');
		
// 		$rows = $this->_model->fetchAll($this->_model->select()->where('advertisement_category_id IN (?)',$id));
// 		if(count($rows)>0){
// 			foreach($rows as $row){
// 				$row->delete();
// 				$status = 1;
// 			}
// 			$this->_helper->flashMessenger('Xóa dữ liệu thành công!');
// 			$this->_helper->redirector('index','advertisementcategory','admin');
// 		}
		$this->_helper->flashMessenger('Chức năng này tạm khóa!');
		$this->_helper->redirector('index','advertisementcategory','admin');
	}
}

